package com.lsh.oms.provider.service.query;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.query.provider.OrderShippingDetailProviderDto;
import com.lsh.oms.api.model.query.provider.OrderShippingHeadProviderDto;
import com.lsh.oms.api.service.query.IqueryOrderShippingService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.exception.ExceptionStatus;
import com.lsh.oms.core.model.order.OrderShippingDetail;
import com.lsh.oms.core.model.order.OrderShippingHead;
import com.lsh.oms.core.service.other.ShippingOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/9/14
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service(protocol = "rest")
@Path("order/shipping/query")
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
@Slf4j
public class QueryOrderShippingServiceImpl implements IqueryOrderShippingService {

    @Autowired
    private ShippingOrderService shippingService;


    @GET
    @Path("getShippingListByProviderId")
    @Override
    public CommonResult<List<OrderShippingHeadProviderDto>> queryShippingHeadsByProviderId(@QueryParam("providerId") String providerId, @QueryParam("size") Integer size) {

        CommonResult<List<OrderShippingHeadProviderDto>> commonResult = new CommonResult<>();

        try {
            if (size == null || size <= 0 || size > 50) {
                throw new BusinessException(ExceptionStatus.E1001001.getCode(), "size 不能为空，必须是1-50的数字");
            }

            if (StringUtils.isEmpty(providerId)) {
                throw new BusinessException(ExceptionStatus.E1001001.getCode(), "providerId 不能为空");
            }

            List<OrderShippingHead> orderShippingHeads = shippingService.selectByProviderId(providerId, true, size);

            if (CollectionUtils.isEmpty(orderShippingHeads)) {
                throw new BusinessException(ExceptionStatus.E1003001.getCode(), ExceptionStatus.E1003001.getMessage());
            }

            List<OrderShippingHeadProviderDto> orderShippingHeadDtos = new ArrayList<>(orderShippingHeads.size());

            for (OrderShippingHead orderShippingHead : orderShippingHeads) {

                OrderShippingHeadProviderDto headProviderDto = new OrderShippingHeadProviderDto();
                BeanUtils.copyProperties(orderShippingHead, headProviderDto);

                List<OrderShippingDetail> orderShippingDetailList = orderShippingHead.getDetails();
                if (CollectionUtils.isEmpty(orderShippingDetailList)) {
                    continue;
                }
                List<OrderShippingDetailProviderDto> items = new ArrayList<>(orderShippingDetailList.size());

                this.builderDeatilDtoList(orderShippingDetailList, items);

                headProviderDto.setItems(items);

                orderShippingHeadDtos.add(headProviderDto);
            }

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage(ExceptionStatus.SUCCESS.getMessage());
            commonResult.setData(orderShippingHeadDtos);
        } catch (BusinessException e) {
            log.error(e.getCode() + ":" + e.getMessage(), e);
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("系统异常 ：" + e.getMessage(), e);
            commonResult.setCode(ExceptionStatus.E1001001.getCode());
            commonResult.setMessage(ExceptionStatus.E1001001.getMessage());
        }

        return commonResult;
    }

    private void builderDeatilDtoList(List<OrderShippingDetail> orderShippingDetailList, List<OrderShippingDetailProviderDto> items) {
        for (OrderShippingDetail orderShippingDetail : orderShippingDetailList) {

            OrderShippingDetailProviderDto detailProviderDto = new OrderShippingDetailProviderDto();
            BeanUtils.copyProperties(orderShippingDetail, detailProviderDto);
            detailProviderDto.setItemNo(orderShippingDetail.getId());

            items.add(detailProviderDto);
        }
    }

    @GET
    @Path("getShippingByOrderCode")
    @Override
    public CommonResult<OrderShippingHeadProviderDto> queryShippingHeadByOrderCode(@QueryParam("orderCode") Long orderCode) {
        CommonResult<OrderShippingHeadProviderDto> commonResult = new CommonResult<>();

        try {

            if (orderCode == null) {
                throw new BusinessException(ExceptionStatus.E1001001.getCode(), "orderCode 不能为空");
            }

            OrderShippingHead orderShippingHead = shippingService.selectOneByOrderCode(orderCode, true);

            if (orderShippingHead == null) {
                throw new BusinessException(ExceptionStatus.E1003001.getCode(), ExceptionStatus.E1003001.getMessage());
            }

            OrderShippingHeadProviderDto headProviderDto = new OrderShippingHeadProviderDto();
            BeanUtils.copyProperties(orderShippingHead, headProviderDto);

            List<OrderShippingDetail> orderShippingDetailList = orderShippingHead.getDetails();
            if (CollectionUtils.isEmpty(orderShippingDetailList)) {
                throw new BusinessException(ExceptionStatus.E1003001.getCode(), ExceptionStatus.E1003001.getMessage());
            }
            List<OrderShippingDetailProviderDto> items = new ArrayList<>(orderShippingDetailList.size());
            builderDeatilDtoList(orderShippingDetailList, items);

            headProviderDto.setItems(items);

            commonResult.setCode(ReturnCode.REQUEST_SUCCESS);
            commonResult.setMessage(ExceptionStatus.SUCCESS.getMessage());
            commonResult.setData(headProviderDto);
        } catch (BusinessException e) {
            log.error(e.getCode() + ":" + e.getMessage(), e);
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("系统异常 ：" + e.getMessage(), e);
            commonResult.setCode(ExceptionStatus.E1001001.getCode());
            commonResult.setMessage(ExceptionStatus.E1001001.getMessage());
        }

        return commonResult;
    }
}
